'''
Created on Jun 18, 2012

@author: Jeremy Tramm
'''

import pygame
import random

black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
blue = (0, 0 , 255)

framesPerSecond = 30
playerSpeed = 6

screen_width = 700
screen_height =400  

class Block(pygame.sprite.Sprite):
    
    def __init__(self, color = black):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([20, 15])
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.dx = 0
        self.dy = 0
        
    def update(self):
        self.rect.move_ip(self.dx, self.dy)

                
class Player(Block):
    
    def __init__(self):
        Block.__init__(self, blue)
        self.link0 = pygame.image.load("../images/link0.png")
        self.link1 = pygame.image.load("../images/link1.png")
        self.link2 = pygame.image.load("../images/link2.png")
        self.link3 = pygame.image.load("../images/link3.png")
        self.flicker = pygame.Surface([16, 22])
        self.flicker.fill(black)
        self.flicker.set_colorkey(black)
        self.image = self.link0
        self.rect = self.image.get_rect()
        self.lives = 3
        self.damageImmunity = 0
        
    def update(self):
        Block.update(self)
        self.updateImage()
        self.damageImmunity -= 1
        if self.rect.x < 0:
            self.rect.x = 0
        elif self.rect.x > screen_width - self.rect.width:
            self.rect.x = screen_width - self.rect.width
        if self.rect.y < 0:
            self.rect.y = 0
        elif self.rect.y > screen_height - self.rect.height:
            self.rect.y = screen_height - self.rect.height  
            
    def updateImage(self):
        if self.damageImmunity > 0 and self.image != self.flicker:
            self.image = self.flicker
        elif self.dx > 0:
            self.image = self.link3
        elif self.dx < 0:
            self.image = self.link2
        elif self.dy > 0:
            self.image = self.link0
        elif self.dy < 0:
            self.image = self.link1
            
    def loseLife(self):
        if self.damageImmunity < 0:
            self.lives -= 1
            if self.lives < 1:
                print("Oh no! you lose!")
                pygame.quit()
                exit()
            self.damageImmunity = 30    
            
class PointBlock(Block):
    
    def __init__(self, dx, dy):
        Block.__init__(self)
        self.image = pygame.image.load("../images/rupee.png")
        self.image.set_colorkey([120, 0, 255])
        self.rect = self.image.get_rect()
        self.dx = dx
        self.dy = dy
        
    def update(self):
        Block.update(self)
        if self.rect.x < 0:
            self.dx = -self.dx
            self.rect.x = 0
        elif self.rect.x > screen_width - self.rect.width:
            self.dx = -self.dx
            self.rect.x = screen_width - self.rect.width
        if self.rect.y < 0:
            self.dy = -self.dy
            self.rect.y = 0
        elif self.rect.y > screen_height - self.rect.height:
            self.dy = -self.dy
            self.rect.y = screen_height - self.rect.height
            
class KillBlock(Block):
    
    def __init__(self, dx, dy):
        Block.__init__(self, red)
        self.image = pygame.image.load("../images/bubble.png")
        self.image.set_colorkey([0,128,255])
        self.rect = self.image.get_rect()
        self.counter = framesPerSecond*10
        self.dx = dx
        self.dy = dy
        
    def update (self):
        Block.update(self)
        self.counter -= 1
        if self.counter == 0:
            self.spawnNewKiller()
            self.counter = framesPerSecond*10
        if self.rect.x < 0:
            self.dx = -self.dx
            self.rect.x = 0
#            self.spawnNewKiller(0)
        elif self.rect.x > screen_width - self.rect.width:
            self.dx = -self.dx
            self.rect.x = screen_width - self.rect.width
#            self.spawnNewKiller(0)
        if self.rect.y < 0:
            self.dy = -self.dy
            self.rect.y = 0
#            self.spawnNewKiller(1)
        elif self.rect.y > screen_height - self.rect.height:
            self.dy = -self.dy
            self.rect.y = screen_height - self.rect.height
#            self.spawnNewKiller(1)
            
    def spawnNewKiller(self, direction=0):
#        if direction == 0:
#            newKiller = KillBlock(self.dx, -self.dy)
#        elif direction == 1:
#            newKiller = KillBlock(-self.dx, self.dy)
        newKiller = KillBlock(-self.dx, -self.dy)
        newKiller.rect.x = self.rect.x
        newKiller.rect.y = self.rect.y
        for group in self.groups():
            group.add(newKiller)
        

pygame.init()
pygame.mixer.init()

screen = pygame.display.set_mode([screen_width, screen_height])

block_list = pygame.sprite.Group()
kill_block_list = pygame.sprite.Group()
all_sprites_list = pygame.sprite.Group()

for i in range(50):
    dx = random.randrange(playerSpeed)
    dy = random.randrange(playerSpeed)
    block = PointBlock(dx, dy)
    
    block.rect.x = random.randrange(screen_width)
    block.rect.y = random.randrange(screen_height)
    
    block_list.add(block)
    all_sprites_list.add(block)
    
for i in range(3):
    dx = random.randrange(1, 4)
    dy = random.randrange(1, 4)
    block = KillBlock(dx, dy)
    
    block.rect.x = random.randrange(screen_width - 20)
    block.rect.y = random.randrange(screen_height - 15)
    
    kill_block_list.add(block)
    all_sprites_list.add(block)
    
player = Player()
all_sprites_list.add(player)
gameMap = pygame.image.load("../images/map.jpg")
gameMap = pygame.transform.scale(gameMap, [screen_width, screen_height])

pygame.font.init()
font = pygame.font.SysFont("Arial", 30, False, False)

hurtsound = pygame.mixer.Sound("../sounds/LTTP_Link_Hurt.ogg")
getsound = pygame.mixer.Sound("../sounds/LTTP_Rupee1.ogg")

clock=pygame.time.Clock()

score = 0

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                player.dy = player.dy - playerSpeed
            if event.key == pygame.K_DOWN:
                player.dy = player.dy + playerSpeed
            if event.key == pygame.K_LEFT:
                player.dx = player.dx - playerSpeed
            if event.key == pygame.K_RIGHT:
                player.dx = player.dx + playerSpeed
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                player.dy = player.dy + playerSpeed
            if event.key == pygame.K_DOWN:
                player.dy = player.dy - playerSpeed
            if event.key == pygame.K_LEFT:
                player.dx = player.dx + playerSpeed
            if event.key == pygame.K_RIGHT:
                player.dx = player.dx - playerSpeed
#        if event.type == pygame.MOUSEBUTTONDOWN:
#            print(event.pos)
#        if event.type == pygame.MOUSEMOTION:
#            player.rect.x, player.rect.y = event.pos
    all_sprites_list.update()
#    screen.fill(white)
    screen.blit(gameMap, [0, 0])
    all_sprites_list.draw(screen)
    screen.blit(font.render("Score: "+str(score), False, [255, 255, 255]), [10, 10])
    screen.blit(font.render("Lives: " + str(player.lives) , False, [255, 255, 255]), [350, 10])
    pygame.display.flip()
    blocks_hit_list = pygame.sprite.spritecollide(player, block_list, True)
    if len(blocks_hit_list) > 0:
        getsound.play()
        score = score + len(blocks_hit_list)
#        print(score)
    kill_blocks_hit_list = pygame.sprite.spritecollide(player, kill_block_list, False)
    if len(kill_blocks_hit_list) > 0:
        if player.damageImmunity < 1:
            hurtsound.play()
        player.loseLife()
    if len(block_list.sprites()) == 0:
        print("You won! Good Jorb!")
        pygame.quit()
        exit()
    clock.tick(framesPerSecond)
